package com.ohos.carbon.slice.widgets;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.*;
import ohos.agp.components.element.ElementScatter;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;

import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;
public class FloatingSlice extends AbilitySlice {

    private Image back_button, y_img, u_img, c_img, u_img4, c_img4;
    private Text text_title, c_text1, c_text2, c_text3, c_text4, c_text5, toast;
    private Component y_layout, x_layout, y_layout1, y_layout2, y_layout3, y_layout4, y_layout5, u_layout, u_layout1, u_layout2, u_layout3, u_layout4, u_layout5, c_layout, c_layout1, c_layout2, c_layout3, c_layout4, c_layout5;
    private boolean layout_y, layout_u, layout_c;
    private AnimatorProperty y_layout1_d, y_layout2_d, y_layout3_d, y_layout4_d, y_layout5_d, u_layout1_d, u_layout2_d, u_layout3_d, u_layout4_d, u_layout5_d, c_layout1_d, c_layout2_d, c_layout3_d, c_layout4_d, c_layout5_d;
    private DirectionalLayout toast_layout;
    private Component toast1;
    private ToastDialog toastDialog;
    private boolean img_y, img_u, img_c;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_floating);
        initView();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Floatin Action Button")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Floatin Action Button")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Floatin Action Button", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Floatin Action Button", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Floatin Action Button", 1);
            pf.flushSync();
        }
    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        y_img = (Image) findComponentById(ResourceTable.Id_y_img);
        u_img = (Image) findComponentById(ResourceTable.Id_u_img);
        c_img = (Image) findComponentById(ResourceTable.Id_c_img);
        u_img4 = (Image) findComponentById(ResourceTable.Id_u_img4);
        c_img4 = (Image) findComponentById(ResourceTable.Id_c_img4);
        x_layout = findComponentById(ResourceTable.Id_x_layout);
        y_layout = findComponentById(ResourceTable.Id_y_layout);
        y_layout1 = findComponentById(ResourceTable.Id_y_layout1);
        y_layout2 = findComponentById(ResourceTable.Id_y_layout2);
        y_layout3 = findComponentById(ResourceTable.Id_y_layout3);
        y_layout4 = findComponentById(ResourceTable.Id_y_layout4);
        y_layout5 = findComponentById(ResourceTable.Id_y_layout5);
        u_layout = findComponentById(ResourceTable.Id_u_layout);
        u_layout1 = findComponentById(ResourceTable.Id_u_layout1);
        u_layout2 = findComponentById(ResourceTable.Id_u_layout2);
        u_layout3 = findComponentById(ResourceTable.Id_u_layout3);
        u_layout4 = findComponentById(ResourceTable.Id_u_layout4);
        u_layout5 = findComponentById(ResourceTable.Id_u_layout5);
        c_layout = findComponentById(ResourceTable.Id_c_layout);
        c_layout1 = findComponentById(ResourceTable.Id_c_layout1);
        c_layout2 = findComponentById(ResourceTable.Id_c_layout2);
        c_layout3 = findComponentById(ResourceTable.Id_c_layout3);
        c_layout4 = findComponentById(ResourceTable.Id_c_layout4);
        c_layout5 = findComponentById(ResourceTable.Id_c_layout5);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        c_text1 = (Text) findComponentById(ResourceTable.Id_c_text1);
        c_text2 = (Text) findComponentById(ResourceTable.Id_c_text2);
        c_text3 = (Text) findComponentById(ResourceTable.Id_c_text3);
        c_text4 = (Text) findComponentById(ResourceTable.Id_c_text4);
        c_text5 = (Text) findComponentById(ResourceTable.Id_c_text5);
        toast = (Text) findComponentById(ResourceTable.Id_toast);
        text_title.setText("Floating Action Button");
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        y_layout1_d = y_layout1.createAnimatorProperty();
        y_layout2_d = y_layout2.createAnimatorProperty();
        y_layout3_d = y_layout3.createAnimatorProperty();
        y_layout4_d = y_layout4.createAnimatorProperty();
        y_layout5_d = y_layout5.createAnimatorProperty();
        u_layout1_d = u_layout1.createAnimatorProperty();
        u_layout2_d = u_layout2.createAnimatorProperty();
        u_layout3_d = u_layout3.createAnimatorProperty();
        u_layout4_d = u_layout4.createAnimatorProperty();
        u_layout5_d = u_layout5.createAnimatorProperty();
        c_layout1_d = c_layout1.createAnimatorProperty();
        c_layout2_d = c_layout2.createAnimatorProperty();
        c_layout3_d = c_layout3.createAnimatorProperty();
        c_layout4_d = c_layout4.createAnimatorProperty();
        c_layout5_d = c_layout5.createAnimatorProperty();
        y_layout1_d.alpha(0).start();
        y_layout2_d.alpha(0).start();
        y_layout3_d.alpha(0).start();
        y_layout4_d.alpha(0).start();
        y_layout5_d.alpha(0).start();
        u_layout1_d.alpha(0).start();
        u_layout2_d.alpha(0).start();
        u_layout3_d.alpha(0).start();
        u_layout4_d.alpha(0).start();
        u_layout5_d.alpha(0).start();
        c_layout1_d.alpha(0).start();
        c_layout2_d.alpha(0).start();
        c_layout3_d.alpha(0).start();
        c_layout4_d.alpha(0).start();
        c_layout5_d.alpha(0).start();

        toast_layout = (DirectionalLayout) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_layout_toast, null, false);
        toast = (Text) toast_layout.findComponentById(ResourceTable.Id_toast);

        x_layout.setClickedListener(component -> {
            y_layout.setVisibility(Component.HIDE);
            u_layout.setVisibility(Component.HIDE);
            c_layout.setVisibility(Component.HIDE);
            layout_y = false;
            layout_u = false;
            layout_c = false;
            img_y = false;
            img_u = false;
            img_c = false;
        });
        y_img.setClickedListener(component -> {
            if (layout_u || layout_c) {
                u_layout1_d.alpha(0).setDelay(0).setDuration(1).start();
                u_layout2_d.alpha(0).setDelay(0).setDuration(1).start();
                u_layout3_d.alpha(0).setDelay(0).setDuration(1).start();
                u_layout4_d.alpha(0).setDelay(0).setDuration(1).start();
                u_layout5_d.alpha(0).setDelay(0).setDuration(1).start();
                layout_u = false;
                c_layout1_d.alpha(0).setDelay(0).setDuration(1).start();
                c_layout2_d.alpha(0).setDelay(0).setDuration(1).start();
                c_layout3_d.alpha(0).setDelay(0).setDuration(1).start();
                c_layout4_d.alpha(0).setDelay(0).setDuration(1).start();
                c_layout5_d.alpha(0).setDelay(0).setDuration(1).start();
                layout_c = false;
            } else {
                if (!layout_y) {
                    y_layout.setVisibility(Component.VISIBLE);
                    y_layout1_d.alpha(1).setDelay(100).start();
                    y_layout2_d.alpha(1).setDelay(150).start();
                    y_layout3_d.alpha(1).setDelay(200).start();
                    y_layout4_d.alpha(1).setDelay(250).start();
                    y_layout5_d.alpha(1).setDelay(300).start();
                    layout_y = true;
                } else {
                    y_layout1_d.alpha(0).setDelay(0).setDuration(1).start();
                    y_layout2_d.alpha(0).setDelay(0).setDuration(1).start();
                    y_layout3_d.alpha(0).setDelay(0).setDuration(1).start();
                    y_layout4_d.alpha(0).setDelay(0).setDuration(1).start();
                    y_layout5_d.alpha(0).setDelay(0).setDuration(1).start();
                    layout_y = false;
                }
            }
        });
        u_img.setClickedListener(component -> {
            if (layout_y || layout_c) {
                y_layout1_d.alpha(0).setDelay(0).setDuration(1).start();
                y_layout2_d.alpha(0).setDelay(0).setDuration(1).start();
                y_layout3_d.alpha(0).setDelay(0).setDuration(1).start();
                y_layout4_d.alpha(0).setDelay(0).setDuration(1).start();
                y_layout5_d.alpha(0).setDelay(0).setDuration(1).start();
                layout_y = false;
                c_layout1_d.alpha(0).setDelay(0).setDuration(1).start();
                c_layout2_d.alpha(0).setDelay(0).setDuration(1).start();
                c_layout3_d.alpha(0).setDelay(0).setDuration(1).start();
                c_layout4_d.alpha(0).setDelay(0).setDuration(1).start();
                c_layout5_d.alpha(0).setDelay(0).setDuration(1).start();
                layout_c = false;
            } else {
                if (!layout_u) {
                    u_layout.setVisibility(Component.VISIBLE);
                    u_layout5_d.alpha(1).setDelay(100).start();
                    u_layout4_d.alpha(1).setDelay(150).start();
                    u_layout3_d.alpha(1).setDelay(200).start();
                    u_layout2_d.alpha(1).setDelay(250).start();
                    u_layout1_d.alpha(1).setDelay(300).start();
                    layout_u = true;
                } else {
                    u_layout1_d.alpha(0).setDelay(0).setDuration(1).start();
                    u_layout2_d.alpha(0).setDelay(0).setDuration(1).start();
                    u_layout3_d.alpha(0).setDelay(0).setDuration(1).start();
                    u_layout4_d.alpha(0).setDelay(0).setDuration(1).start();
                    u_layout5_d.alpha(0).setDelay(0).setDuration(1).start();
                    layout_u = false;
                }
            }
        });
        c_img.setClickedListener(component -> {
            if (layout_y || layout_u) {
                y_layout1_d.alpha(0).setDelay(0).setDuration(1).start();
                y_layout2_d.alpha(0).setDelay(0).setDuration(1).start();
                y_layout3_d.alpha(0).setDelay(0).setDuration(1).start();
                y_layout4_d.alpha(0).setDelay(0).setDuration(1).start();
                y_layout5_d.alpha(0).setDelay(0).setDuration(1).start();
                layout_y = false;
                u_layout1_d.alpha(0).setDelay(0).setDuration(1).start();
                u_layout2_d.alpha(0).setDelay(0).setDuration(1).start();
                u_layout3_d.alpha(0).setDelay(0).setDuration(1).start();
                u_layout4_d.alpha(0).setDelay(0).setDuration(1).start();
                u_layout5_d.alpha(0).setDelay(0).setDuration(1).start();
                layout_u = false;
            } else {
                if (!layout_c) {
                    c_layout.setVisibility(Component.VISIBLE);
                    c_layout5_d.alpha(1).setDelay(100).start();
                    c_layout4_d.alpha(1).setDelay(150).start();
                    c_layout3_d.alpha(1).setDelay(200).start();
                    c_layout2_d.alpha(1).setDelay(250).start();
                    c_layout1_d.alpha(1).setDelay(300).start();
                    layout_c = true;
                } else {
                    c_layout1_d.alpha(0).setDelay(0).setDuration(1).start();
                    c_layout2_d.alpha(0).setDelay(0).setDuration(1).start();
                    c_layout3_d.alpha(0).setDelay(0).setDuration(1).start();
                    c_layout4_d.alpha(0).setDelay(0).setDuration(1).start();
                    c_layout5_d.alpha(0).setDelay(0).setDuration(1).start();
                    layout_c = false;
                }
            }
        });

        c_layout1.setClickedListener(component -> {
            new ToastDialog(getContext())
                    .setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                    .setText(c_text1.getText())
                    .setAlignment(LayoutAlignment.BOTTOM)
                    .show();
            c_layout.setVisibility(Component.HIDE);
            layout_c = false;
        });
        c_layout2.setClickedListener(component -> {
            new ToastDialog(getContext())
                    .setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                    .setText(c_text2.getText())
                    .setAlignment(LayoutAlignment.BOTTOM)
                    .show();
            c_layout.setVisibility(Component.HIDE);
            layout_c = false;
        });
        c_layout3.setClickedListener(component -> {
            new ToastDialog(getContext())
                    .setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                    .setText(c_text3.getText())
                    .setAlignment(LayoutAlignment.BOTTOM)
                    .show();
            c_layout.setVisibility(Component.HIDE);
            layout_c = false;
        });
        c_layout4.setClickedListener(component -> {
            new ToastDialog(getContext())
                    .setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                    .setText(c_text4.getText())
                    .setAlignment(LayoutAlignment.BOTTOM)
                    .show();
            c_layout.setVisibility(Component.HIDE);
            layout_c = false;
        });
        c_layout5.setClickedListener(component -> {
            new ToastDialog(getContext())
                    .setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                    .setText(c_text5.getText())
                    .setAlignment(LayoutAlignment.BOTTOM)
                    .show();
            c_layout.setVisibility(Component.HIDE);
            layout_c = false;
        });
        back_button.setClickedListener(component -> onBackPressed());
    }

    @Override
    protected void onBackground() {
        super.onBackground();
        y_layout.setVisibility(Component.HIDE);
        u_layout.setVisibility(Component.HIDE);
        c_layout.setVisibility(Component.HIDE);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
